package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.PlatformInfo;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsPalletSortingMapper;
import com.jy.wms.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: WmsPalletSortingService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author ln
 * @Date Mon Sep 25 17:10:00 CST 2017
 */

@Service
public class WmsPalletSortingService extends BaseService<WmsPalletSortingMapper,WmsPalletSorting> {

    /**
     * 插入一条记录
     * @param entity 实体对象
     * @param database 数据库名称
     * @return int
     */
    public Integer baseInsert( WmsPalletSorting entity, String database ){
        return dao.baseInsert( entity,database );
    }

    /**
     * 插入一条记录（匹配有值的字段）
     * @param entity 实体对象
     * @param database 数据库名称
     * @return int
     */
    public Integer baseInsertSelective( WmsPalletSorting entity,String database ){
        return dao.baseInsertSelective( entity,database );
    }

    /**
     * 删除（根据主键ID删除）
     * @param id 主键
     * @param database 数据库名称
     * @return int
     */
    public Integer baseDeleteByPrimaryKey( String id,String database ){
        return dao.baseDeleteByPrimaryKey( id,database );
    }

    /**
     * 码盘 (批量插入)
     */
    @BaseServiceDescribe(moduleName = "添加码盘", btnName = "添加码盘")
    @Transactional
    public List baseInsertPlSelective(List<String> ids,String ckid)throws ServiceException {
        List<WmsEstimatedArrivalOrder> zt = dao.baseSelectZt(ids); //根据ids查询asn订单数据
        for (int i = 0; i < zt.size(); i++) {
            if (!("0".equals(zt.get(i).getAsnzt()) && "1".equals(zt.get(i).getShzt()) &&  "0".equals(zt.get(i).getMpzt()))) {
                throw new ServiceException("码盘失败,所选订单不符合码盘要求");
            }
        }
        List<String> idss = new ArrayList<String>();
        List<WmsEstimatedArrivalOrderDetailed> welist = dao.baseSelectMpMx(ids);//查询预期到货通知单明细
        List<WmsEstimatedArrivalOrderDetailed> zyqsls = dao.baseSelectZyqsl(ids);
        List<WmsPalletSorting> listInsert = new ArrayList<WmsPalletSorting>();
        StringBuffer sb=new StringBuffer();//
        int yqSj = 0;
        int bzSj = 0;
        int zyqsl = 0;
        if (welist.size() > 0) {
            for (int k = 0; k < welist.size(); k++) {
                yqSj = Integer.parseInt(welist.get(k).getYqsl()); //预期到货通知单明细主单位数量
                //根据明细货品 查询包装计算托盘数量
                String hpid = welist.get(k).getHpid();
                WmsPackingDetailed wpd = dao.DwSelectId(hpid);//根据每条明细货品查询包装明细主单位
                if (wpd != null) {//判断主单位数量是否为空  如果为空 不码盘 提示为空数据的ASN编码 和货品名称
                    // if (wpd.getZdwsl() != null && !("").equals(wpd.getZdwsl())) {//因主单位数量不可为空 注掉  //判断主单位数量是否为空  如果为空 不码盘 提示为空数据的ASN编码 和货品名称
                    bzSj = Integer.parseInt(wpd.getZdwsl());//包装主单位数量
                    int slt = yqSj / bzSj;
                    int sly = yqSj % bzSj;
                    WmsPalletSorting wpdd = null;
                    for (int j = 0; j < slt; j++) {
                        wpdd = new WmsPalletSorting();
                        int lpnh = selectlpn();
                        wpdd.setId(UUIDUtils.uuid());
                        wpdd.setYqdmxid(welist.get(k).getId()); //000
                        wpdd.setYqmpsl(String.valueOf(bzSj));
                        wpdd.setCkid(ckid);
                        wpdd.setSssl("0");
                        wpdd.setLpn(String.format("%015d", lpnh));
                        wpdd.setTh(wpd.getTh());
                        listInsert.add(wpdd);
                    }
                    if (sly > 0) {
                        WmsPalletSorting wpds = new WmsPalletSorting();
                        int lpnh = selectlpn();
                        wpds.setId(UUIDUtils.uuid());
                        wpds.setYqdmxid(welist.get(k).getId());
                        wpds.setYqmpsl(String.valueOf(sly));
                        wpds.setCkid(ckid);
                        wpds.setSssl("0");
                        wpds.setLpn(String.format("%015d", lpnh));
                        wpds.setTh(wpd.getTh());
                        listInsert.add(wpds);
                    }
               /* } else {
                    throw new ServiceException("ASN编号:" + welist.get(0).getAsnbh() + "货品名称:" + wpd.getMc() + ",包装主单位不能为空,码盘失败");
                }*/
                }else{
                   // sb.append("asn编号:"+welist.get(0).getAsnbh());
                    throw new ServiceException("主单位数量不能为空,asn编号:"+welist.get(0).getAsnbh());
                }
            }
            int cg = dao.baseInsertPlSelective(listInsert);  //批量添加
            if (cg > 0) {
                for (int ii = 0; ii < ids.size(); ii++) {
                    List<WmsPalletSorting> mpzs = dao.YqSelectMxId(ids);//根据预期到货id 查询该明细总数量
                    // if (ids.get(ii).equals(mpzs.get(0).getSsyqdid())){//判断单据id与码盘id是否相等
                    zyqsl = Integer.parseInt(zyqsls.get(0).getZyqsl());
                    System.out.println(zyqsl);
                    System.out.println(mpzs.get(0).getMpsl());
                    if (zyqsl == mpzs.get(0).getMpsl()) {
                        dao.baseUpdateBathPrimaryKeys(ids.get(ii), "1");//修改单据 码盘状态
                    } else {
                        List<WmsPalletSorting> mx = dao.SelectMxId(ids.get(ii));//查询ASN 明细id
                        int i = 0;
                        for (int kk = 0; kk < mx.size(); kk++) {
                            i = dao.baseDeleteBathPrimaryKeys(mx.get(kk).getId()); //根据ids删除码盘表明细
                        }
                        System.out.println(welist.get(ii).getAsnbh());
                        idss.add(welist.get(ii).getAsnbh()+","+sb+"包装主单位不能为空,码盘失败");
                    }
                }
            }
        }
        return idss;
    }

    /**
     *  在conction 中根据货主判读上架规则 如果上架规则为002 走此方法
     * 码盘002
     */
    public List baseInsertSelective(List<String> ids,String ckid,String hzid)throws ServiceException {
        //查询ASN订单->ASN订单明细->ASN订单明细货品->预期数量
        List<WmsEstimatedArrivalOrder> zt = dao.baseSelectZt(ids); //根据ids查询asn订单数据 判断状态是否符合码盘要求
        for (int i = 0; i < zt.size(); i++) {
            if (!("0".equals(zt.get(i).getAsnzt()) && "1".equals(zt.get(i).getShzt()) &&  "0".equals(zt.get(i).getMpzt()))) {
                throw new ServiceException("码盘失败,所选订单不符合码盘要求");
            }
        }
        StringBuffer sb=new StringBuffer();//
        String cs=null;//库位高度
        int zdwsl=0;//堆码主单位数量
        int tps=0;//库位托盘数
        int hpsl=0;//货品数量
        int zyqsl = 0;
        List<String>  kwlist=new ArrayList<String>();//存放查询出库位id
        List<String> idss = new ArrayList<String>();
        List<WmsEstimatedArrivalOrderDetailed> welist = dao.baseSelectMpMx(ids);//查询预期到货通知单明细
        List<WmsEstimatedArrivalOrderDetailed> zyqsls = dao.baseSelectZyqsl(ids);
        List<WmsPalletSorting> listInsert = new ArrayList<WmsPalletSorting>();
        for (int i=0;i<welist.size();i++) {
            System.out.println(welist.get(i).getHpid());
            hpsl = Integer.parseInt(welist.get(i).getYqsl()); //预期到货通知单明细主单位数量(货品数量)
            System.out.println(welist.get(i).getHpid());
            System.out.println(hzid);
            List<WmsGoodsLocation> group = dao.bdGroup(welist.get(i).getHpid(),hzid);//根据ASN 订单明细货品id查询是否绑定库位组
            if (group.size() > 0) {//如果绑定库位组，查询库位中占用状态为未占用的库位
                //1.根据库位组id 查询库位,在库位中查询出状态为“正常”“管控”的库位，
                // 根据找出来的库位进行排序(库位表中找到层数和库位 进行排序(先排序层数后排序库位))找第一个库位高度
                for (int k = 0; k < group.size(); k++) {//循环库位组
                    String zyztw= PlatformInfo.zyztw.getType();
                    String lx=PlatformInfo.lx.getType();
                    List<WmsLibraryPosition> listkw = dao.library(group.get(k).getKwzid(),zyztw,lx,"0",hzid);//根据货品查询出的第一个库位组查询库位
                    //3.根据找到的库位高度与包装明细中的堆码进行对比
                    for (int j = 0; j < listkw.size(); j++) {//找到第一个库位的高度
                        cs = listkw.get(j).getCs(); //库位高度
                        tps = Integer.parseInt(listkw.get(j).getTps());//库位托盘数
                        List<WmsStacking> stacking = dao.dyStacking(welist.get(i).getHpid(),listkw.get(j).getKwgd());//根据ASN 订单明细货品id 查询堆码 (传入库位cs 匹配堆码)
                        if (stacking.size()==0){//如果堆码为空,不可码盘
                            throw new RuntimeException("库位高度与堆码库位高度不相等,库位名称："+listkw.get(j).getMc()+"库位高度："+listkw.get(j).getKwgd());
                        }
                        zdwsl=stacking.get(0).getZsl();
                            //相同
                                WmsPalletSorting wpdd = null;
                                for (int t = 0; t < tps; t++) {//循环库位托盘码数量数量
                                    if (hpsl > 0) {
                                        //根据堆码主单位数量进行码盘码盘
                                        wpdd = new WmsPalletSorting();
                                        int lpnh = selectlpn();
                                        wpdd.setId(UUIDUtils.uuid());
                                        wpdd.setYqdmxid(welist.get(i).getId()); //000
                                        if (hpsl<zdwsl){
                                            wpdd.setYqmpsl(String.valueOf(hpsl));
                                            hpsl = hpsl - hpsl;
                                        }else {
                                            wpdd.setYqmpsl(String.valueOf(zdwsl));
                                            hpsl = hpsl - zdwsl;
                                        }
                                        wpdd.setCkid(ckid);
                                        wpdd.setSssl("0");
                                        wpdd.setLpn(String.format("%015d", lpnh));
                                        wpdd.setKwid(listkw.get(j).getId());
                                        wpdd.setTh(stacking.get(0).getTh());
                                        listInsert.add(wpdd);
                                        String zyzt= PlatformInfo.zyzty.getType();
                                        dao.updatedt(listkw.get(j).getId(),zyzt); //修改库位占用状态
                                        kwlist.add(listkw.get(j).getId());//库位id
                                        if (hpsl==0){
                                            break;
                                        }
                                    }
                                }
                        }
                }
                //如果没有可用库位，按照标准托盘数量放置虚拟存储库位
                if (hpsl>0){
                    WmsPackingDetailed wpd = dao.DwSelectId(welist.get(i).getHpid());//根据每条明细货品查询包装明细主单位
                    if(wpd!=null){
                        int bzSj = Integer.parseInt(wpd.getZdwsl());//包装主单位数量
                        int slt = hpsl / bzSj;
                        int sly = hpsl % bzSj;
                        WmsPalletSorting wpdd = null;
                        for (int j = 0; j < slt; j++) {
                            wpdd = new WmsPalletSorting();
                            int lpnh = selectlpn();
                            wpdd.setId(UUIDUtils.uuid());
                            wpdd.setYqdmxid(welist.get(i).getId()); //000
                            wpdd.setYqmpsl(String.valueOf(bzSj));
                            wpdd.setCkid(ckid);
                            wpdd.setSssl("0");
                            wpdd.setLpn(String.format("%015d", lpnh));
                            wpdd.setTh(wpd.getTh());
                            listInsert.add(wpdd);
                        }
                        if (sly > 0) {
                            WmsPalletSorting wpds = new WmsPalletSorting();
                            int lpnh = selectlpn();
                            wpds.setId(UUIDUtils.uuid());
                            wpds.setYqdmxid(welist.get(i).getId());
                            wpds.setYqmpsl(String.valueOf(sly));
                            wpds.setCkid(ckid);
                            wpds.setSssl("0");
                            wpds.setLpn(String.format("%015d", lpnh));
                            wpds.setTh(wpd.getTh());
                            listInsert.add(wpds);
                        }
                    }else{
                        //sb.append("asn编号:"+welist.get(0).getAsnbh());
                        throw new ServiceException("主单位数量不能为空,asn编号:"+welist.get(0).getAsnbh());
                    }
                }
                }else {//未绑定库位组，查询库位类型为"存储库位"，"占用状态"为未占用
                //1.根据库位组id 查询库位,在库位中查询出状态为“正常”“管控”的库位，
                // 根据找出来的库位进行排序(库位表中找到层数和库位 进行排序(先排序层数后排序库位))找第一个库位高度
               /* for (int k = 0; k < group.size(); k++) {//循环库位组*/
                    String zyztw= PlatformInfo.zyztw.getType();
                    String lx=PlatformInfo.lx.getType();
                    List<WmsLibraryPosition> listkw = dao.library("",zyztw,lx,"1",hzid);//根据货品查询出库位(未绑定库位组)
                    //3.根据找到的库位高度与包装明细中的堆码进行对比
                    for (int j = 0; j < listkw.size(); j++) {//找到第一个库位的高度
                        System.out.println(listkw.get(j).getId()+"==========");
                        cs = listkw.get(j).getCs(); //库位高度
                       // kwlist.add(listkw.get(j).getId());//库位id
                        tps = Integer.parseInt(listkw.get(j).getTps());//库位托盘数
                        List<WmsStacking> stacking = dao.dyStacking(welist.get(i).getHpid(),listkw.get(j).getKwgd());//根据ASN 订单明细货品id 查询堆码 (传入库位cs 匹配堆码)
                        if (stacking.size()==0){//如果堆码为空,不可码盘
                            throw new RuntimeException("库位高度与堆码库位高度不相等,库位名称:"+listkw.get(j).getMc()+"库位高度："+listkw.get(j).getKwgd());
                        }
                        zdwsl=stacking.get(0).getZsl();
                        //相同
                        WmsPalletSorting wpdd = null;
                        for (int t = 0; t < tps; t++) {//循环库位托盘码数量数量
                            if (hpsl > 0) {
                                //根据堆码主单位数量进行码盘码盘
                                wpdd = new WmsPalletSorting();
                                int lpnh = selectlpn();
                                wpdd.setId(UUIDUtils.uuid());
                                wpdd.setYqdmxid(welist.get(i).getId()); //000
                                if (hpsl<zdwsl){
                                    wpdd.setYqmpsl(String.valueOf(hpsl));
                                    hpsl = hpsl - hpsl;
                                }else {
                                    wpdd.setYqmpsl(String.valueOf(zdwsl));
                                    hpsl = hpsl - zdwsl;
                                }
                                wpdd.setCkid(ckid);
                                wpdd.setSssl("0");
                                wpdd.setLpn(String.format("%015d", lpnh));
                                wpdd.setKwid(listkw.get(j).getId());
                                wpdd.setTh(stacking.get(0).getTh());
                                listInsert.add(wpdd);
                                String zyzt= PlatformInfo.zyzty.getType();
                                dao.updatedt(listkw.get(j).getId(),zyzt); //修改库位占用状态
                                kwlist.add(listkw.get(j).getId());//库位id
                                if (hpsl==0){
                                    break;
                                }
                            }
                        }
                        if (hpsl==0){
                            break;
                        }
                    }
               /* }*/
                //如果没有可用库位，按照标准托盘数量放置虚拟存储库位
                if (hpsl>0){
                    WmsPackingDetailed wpd = dao.DwSelectId(welist.get(i).getHpid());//根据每条明细货品查询包装明细主单位
                    if (wpd!=null){
                        int bzSj = Integer.parseInt(wpd.getZdwsl());//包装主单位数量
                        int slt = hpsl / bzSj;
                        int sly = hpsl % bzSj;
                        WmsPalletSorting wpdd = null;
                        for (int j = 0; j < slt; j++) {
                            wpdd = new WmsPalletSorting();
                            int lpnh = selectlpn();
                            wpdd.setId(UUIDUtils.uuid());
                            wpdd.setYqdmxid(welist.get(i).getId()); //000
                            wpdd.setYqmpsl(String.valueOf(bzSj));
                            wpdd.setCkid(ckid);
                            wpdd.setSssl("0");
                            wpdd.setLpn(String.format("%015d", lpnh));
                            wpdd.setTh(wpd.getTh());
                            listInsert.add(wpdd);
                        }
                        if (sly > 0) {
                            WmsPalletSorting wpds = new WmsPalletSorting();
                            int lpnh = selectlpn();
                            wpds.setId(UUIDUtils.uuid());
                            wpds.setYqdmxid(welist.get(i).getId());
                            wpds.setYqmpsl(String.valueOf(sly));
                            wpds.setCkid(ckid);
                            wpds.setSssl("0");
                            wpds.setLpn(String.format("%015d", lpnh));
                            wpds.setTh(wpd.getTh());
                            listInsert.add(wpds);
                        }
                    }else {
                       // sb.append("asn编号:"+welist.get(0).getAsnbh());
                        throw new ServiceException("主单位数量不能为空,asn编号:"+welist.get(0).getAsnbh());
                    }
                }

                }
            }
        int cg = dao.baseInsertPlSelective(listInsert);  //批量添加
        if (cg > 0) {
            for (int ii = 0; ii < ids.size(); ii++) {
                List<WmsPalletSorting> mpzs = dao.YqSelectMxId(ids);//根据预期到货id 查询该明细总数量
                zyqsl = Integer.parseInt(zyqsls.get(0).getZyqsl());
                System.out.println(zyqsl);
                System.out.println(mpzs.get(0).getMpsl());
                if (zyqsl == mpzs.get(0).getMpsl()) {
                    dao.baseUpdateBathPrimaryKeys(ids.get(ii), "1");//修改单据 码盘状态
                  /*  if (kwlist.size()>0){//如果绑定库位 修改库位占用状态
                        String zyzt= PlatformInfo.zyzty.getType();
                        dao.updateku(kwlist,zyzt); //修改库位占用状态
                    }*/
                } else {
                    List<WmsPalletSorting> mx = dao.SelectMxId(ids.get(ii));//查询ASN 明细id
                    int ss = 0;
                    for (int kk = 0; kk < mx.size(); kk++) {
                        ss= dao.baseDeleteBathPrimaryKeys(mx.get(kk).getId()); //根据ids删除码盘表明细
                        String zyzt= PlatformInfo.zyztw.getType();
                        dao.updateku(kwlist,zyzt); //修改库位占用状态
                    }
                    System.out.println(welist.get(ii).getAsnbh());
                    idss.add(welist.get(ii).getAsnbh()+","+sb+"包装主单位不能为空,码盘失败");
                }
            }
        }
        return idss;
        }

    /**
     * 根据货主id 查询上架规则
     */
    public String shelf(String hzid) throws ServiceException{
        String shelf=dao.shelf(hzid);
        if (shelf.equals("")){
            throw new ServiceException("上架规则查询失败");
        }
        return shelf;
    }
    /**
     * 分页查询
     * @param entity 实体
     * @return Page<WmsPalletSorting>
     */
    @BaseServiceDescribe(moduleName = "码盘",btnName = "分页查询")
    public Page<WmsEstimatedArrivalOrder> baseSelectPage(WmsEstimatedArrivalOrder entity ) throws ServiceException{
        Page<WmsEstimatedArrivalOrder> list=null;
        try{
            PageHelper.startPage(entity.getPage(),entity.getRows());
            list=(Page<WmsEstimatedArrivalOrder>) dao.baseSelectPage(entity);
            return list;
        }catch (Exception e){
            throw new ServiceException("码盘分页查询失败",e);
        }
    }


    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList   主键ID列表
     * @return int
     */
    @BaseServiceDescribe(moduleName = "取消码盘", btnName = "取消码盘")
    @Transactional
    public void baseDeleteBathPrimaryKeys(List<String> idList,String mpzt) throws ServiceException {
        List<String> kwlist=new ArrayList<String>();
        List<String> listk=new ArrayList<String>();//存放查询出库位id
        List<WmsEstimatedArrivalOrder> zt = dao.baseSelectZt(idList);  //根据ids查询asn订单数据
        for (int j=0;j<zt.size();j++){
            if (!("0".equals(zt.get(j).getAsnzt()) && "1".equals(zt.get(j).getMpzt()))){
                throw new ServiceException("取消码盘失败,所选订单不符合取消码盘要求");
            }
        }
        for (int i=0;i<idList.size();i++){
            List<WmsPalletSorting> mx= dao.SelectMxId(idList.get(i));//查询ASN 明细id
            for (int j=0;j<mx.size();j++) {
                kwlist=dao.kwSelect(mx.get(j).getId()); //根据id 查询kwid预期到货明细ID
                Integer b = dao.baseDeleteBathPrimaryKeys(mx.get(j).getId());
                if (b > 0) {
                    dao.baseUpdateBathPrimaryKeys(idList.get(i), mpzt);
                   if(kwlist.size()>0){//如果存在 修改库位占用状态
                       for (int k=0;k<kwlist.size();k++){
                           listk.add(kwlist.get(k));
                           String zyzt= PlatformInfo.zyztw.getType();
                           dao.updateku(listk,zyzt); //修改库位占用状态
                       }
                    }
                } else {
                    throw new ServiceException("取消码盘失败");
                }
            }
        }
    }

    /**
     * 明细查询(根跟单据查询明细)
     */
    @BaseServiceDescribe(moduleName = "明细查询" ,btnName = "查询明细")
    public Page<WmsPalletSorting> baseSelectMx(String ssyqdid,Integer page,Integer rows) throws ServiceException{
        Page<WmsPalletSorting> list=null;
        try {
            PageHelper.startPage(page,rows);
            list=dao.baseSelectMx(ssyqdid);
            return list;
        }catch (Exception e){
            throw new ServiceException("明细查询失败",e);
        }
    }

    /**
     * 根据勾选的码盘明细打印
     */
    @BaseServiceDescribe(moduleName = "根据勾选的码盘明细打印" ,btnName = "根据勾选的码盘明细打印")
    public List<WmsPalletSorting> baseDyid(List<String> ids) throws ServiceException{
        List<WmsPalletSorting> list=null;
        try {
            list=dao.baseDyid(ids);
            return list;
        }catch (Exception e){
            throw new ServiceException("码盘明细打印失败",e);
        }
    }

    /**
     * 打印托盘码
     */
    @BaseServiceDescribe(moduleName = "打印托盘码" ,btnName = "打印托盘码")
    public List<WmsPalletSorting> dySelecttp(List<String> ids)throws ServiceException{
        List<WmsPalletSorting> lists=null;
        try {
            lists=dao.dySelecttp(ids);
            if (lists.size()>0){
                if ( lists.get(0).getMpdysj()==null){
                    //修改打印时间
                    dao.baseUpdateTp(ids);
                }
            }
            return lists;
        }catch (Exception e){
            throw new ServiceException("码盘明细打印失败",e);
        }

    }

    /**
     * 查询单据id查询明细（根据ID 批量查询）
     * @param idList 主键ID列表
     * @param database 数据库名称
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<String> idList, String database ){
        return dao.baseSelectBathPrimaryKeys( idList,database );
    }


    /**
     * 查询（根据主键ID查询）
     * @param id 主键ID
     * @param database 数据库名称
     * @return WmsPalletSorting
     */
    public WmsPalletSorting baseSelectByPrimaryKey( String id,String database ){
        return dao.baseSelectByPrimaryKey( id,database );
    }

    /**
     * 查询（根据ID 批量查询）
     * @param idList 主键ID列表
     * @param database 数据库名称
     * @return list
     */
    /*public Integer baseSelectBathPrimaryKeys( List<WmsPalletSorting> idList,String database ){
         return dao.baseSelectBathPrimaryKeys( idList,database );
    }*/

    /**
     *修改 （匹配有值的字段）
     * @param list 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective( List<WmsPalletSorting> list){
        return dao.baseUpdateByPrimaryKeySelective(list );
    }

    /**
     *修改（根据主键ID修改）
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey( WmsPalletSorting entity){
        return dao.baseUpdateByPrimaryKey( entity);
    }
    /**
     * 分页查询（根据预期通知单ID查询）
     * @param ssyqdid 预期通知单ID
     * @return  List<WmsPalletSorting>
     */
    @BaseServiceDescribe(moduleName = "码盘收货",btnName = "分页查询码盘收货明细")
    public Page<WmsPalletSorting> baseSelectBySsyqdid(String ssyqdid,Integer num, Integer size)throws ServiceException {
        Page<WmsPalletSorting> list=null;
        PageHelper.startPage(num,size);
        try {
            list = (Page<WmsPalletSorting>)dao.baseSelectBySsyqdid(ssyqdid);
            return list;
        }catch (Exception e) {
            throw  new ServiceException("查询码盘收货情况失败",e);
        }

    }
    public int selectlpn() {
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {//有可能是负数
            hashCodeV = - hashCodeV;
        }
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        return hashCodeV;
    }
    /**
     *查询（查询lpn是否存在）
     * @return int
     */
    public String selectlpnBylpn()throws  ServiceException{
        int num;
        int fnum;
        String lpn;
        try {
            do {
                Random random=new Random();
                num = random.nextInt(9999-1000+1)+1000;
                SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
                Date time = new Timestamp(System.currentTimeMillis());
                String lpnno = sdf.format(time);
                lpn =lpnno + num;
                fnum = dao.selectlpnBylpn(lpn);
            } while (fnum != 0);
            return lpn;
        } catch (Exception e) {
            throw new ServiceException("生成lpn单号失败",e);
        }
    }
}
